package com.tledu.course.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.tledu.course.domain.SonSection;
import com.tledu.course.mapper.SectionManagementMapper;
import com.tledu.course.domain.SectionManagement;
import com.tledu.course.service.ISectionManagementService;
import com.ruoyi.common.core.text.Convert;

/**
 * 章节管理Service业务层处理
 * 
 * @author Ghost
 * @date 2021-07-13
 */
@Service
public class SectionManagementServiceImpl implements ISectionManagementService 
{
    @Autowired
    private SectionManagementMapper sectionManagementMapper;

    /**
     * 查询章节管理
     * 
     * @param sectionId 章节管理ID
     * @return 章节管理
     */
    @Override
    public SectionManagement selectSectionManagementById(Integer sectionId)
    {
        return sectionManagementMapper.selectSectionManagementById(sectionId);
    }

    /**
     * 查询章节管理列表
     * 
     * @param sectionManagement 章节管理
     * @return 章节管理
     */
    @Override
    public List<SectionManagement> selectSectionManagementList(SectionManagement sectionManagement)
    {
        return sectionManagementMapper.selectSectionManagementList(sectionManagement);
    }

    /**
     * 新增章节管理
     * 
     * @param sectionManagement 章节管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSectionManagement(SectionManagement sectionManagement)
    {
        sectionManagement.setCreateTime(DateUtils.getNowDate());
        int rows = sectionManagementMapper.insertSectionManagement(sectionManagement);
        insertSonSection(sectionManagement);
        return rows;
    }

    /**
     * 修改章节管理
     * 
     * @param sectionManagement 章节管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSectionManagement(SectionManagement sectionManagement)
    {
        sectionManagement.setUpdateTime(DateUtils.getNowDate());
        sectionManagementMapper.deleteSonSectionBySectionId(sectionManagement.getSectionId());
        insertSonSection(sectionManagement);
        return sectionManagementMapper.updateSectionManagement(sectionManagement);
    }

    /**
     * 删除章节管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSectionManagementByIds(String ids)
    {
        sectionManagementMapper.deleteSonSectionBySectionIds(Convert.toStrArray(ids));
        return sectionManagementMapper.deleteSectionManagementByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除章节管理信息
     * 
     * @param sectionId 章节管理ID
     * @return 结果
     */
    @Override
    public int deleteSectionManagementById(Long sectionId)
    {
        sectionManagementMapper.deleteSonSectionBySectionId(sectionId);
        return sectionManagementMapper.deleteSectionManagementById(sectionId);
    }

    /**
     * 新增子章节信息
     * 
     * @param sectionManagement 章节管理对象
     */
    public void insertSonSection(SectionManagement sectionManagement)
    {
        List<SonSection> sonSectionList = sectionManagement.getSonSectionList();
        Long sectionId = (long)sectionManagement.getSectionId();
        if (StringUtils.isNotNull(sonSectionList))
        {
            List<SonSection> list = new ArrayList<SonSection>();
            for (SonSection sonSection : sonSectionList)
            {
                sonSection.setSectionId(sectionId);
                list.add(sonSection);
            }
            if (list.size() > 0)
            {
                sectionManagementMapper.batchSonSection(list);
            }
        }
    }
}
